﻿using System;
using System.Globalization;

namespace Quail.DbLogger.Task
{
	/// <summary>
	/// Wraps Inserting a new TestRunTask
	/// </summary>
    public class InsertTestRunTask : QuailDbLoggerEntry
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InsertTestRunTask"/> class.
        /// </summary>
        /// <param name="parenttestruntaskid">The parenttestruntaskid.</param>
        /// <param name="testrunid">The testrunid.</param>
        /// <param name="taskname">The taskname.</param>
        /// <param name="taskstatus">The taskstatus.</param>
        /// <param name="callback">callback when the TestRunTaskId is compelte</param>
        /// <param name="startDate">the start date</param>
        public InsertTestRunTask(long? parenttestruntaskid, int testrunid, string taskname, LogStatus taskstatus, InsertTestRunTaskComplete callback, DateTime startDate)
        {
            TaskStartDate = startDate;

            ParentTestRunTaskId = parenttestruntaskid;
            TestRunId = testrunid;
            TaskName = taskname;
            TaskStatus = taskstatus;

            OnInsertTestRunTaskComplete += callback;
        }

        /// <summary>
        /// Callback method for when the insert test run taks is complete
        /// </summary>
        /// <param name="testrunruntaskid">the new testrunruntaskid</param>
        public delegate void InsertTestRunTaskComplete(long testrunruntaskid);

        /// <summary>
        /// Called when the store is complete
        /// </summary>
        public event InsertTestRunTaskComplete OnInsertTestRunTaskComplete;

        private long? ParentTestRunTaskId { get; set; }

        private int TestRunId { get; set; }

        private string TaskName { get; set; }

        private LogStatus TaskStatus { get; set; }

        private DateTime TaskStartDate { get; set; }

        /// <summary>
        /// Inserts a new test run task
        /// </summary>
        /// <param name="dataContext">the context to use</param>
        public override void Store(QuailServices.QuailDataServiceReference.QuailDataServiceClient dataContext)
        {
            var testRunTaskId = dataContext.StartTestRunTask(ParentTestRunTaskId, TestRunId, TaskName, (int)TaskStatus, TaskStartDate);
            if (OnInsertTestRunTaskComplete != null)
                OnInsertTestRunTaskComplete(testRunTaskId);
        }

        /// <summary>
        /// Returns a string representation of the entry for logging purposes
        /// </summary>
        /// <returns>A log string</returns>
        public override string GetLogDetail()
        {
            return string.Format("InsertTestRunTask({0},{1},{2},{3},{4})",
                                 ParentTestRunTaskId.HasValue ? ParentTestRunTaskId.Value.ToString(CultureInfo.InvariantCulture) : "NULL", TestRunId,
                                 TaskName, TaskStatus, TaskStartDate);
        }
    }
}
